package com.tosit.bmx.service.user.Impl;

import com.tosit.bmx.domian.ErrorTopic;
import com.tosit.bmx.domian.ErrorTopicVo;
import com.tosit.bmx.domian.TempSubject;
import com.tosit.bmx.mapper.ErrorTopicMapper;
import com.tosit.bmx.mapper.TemporaryMapper;
import com.tosit.bmx.service.user.ErrorService;
import com.tosit.bmx.service.user.TemporaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author tsl
 * @create 2018-04-27 15:31
 **/
@Service
public class ErrorServiceImpl implements ErrorService {
    @Autowired
    ErrorTopicMapper topicMapper;
    @Autowired
    TemporaryMapper temporaryMapper;
    /**
     * 根据用户id获取所有错题
     *
     * @param
     * @return
     * @author tsl
     * @date 2018/4/26 17:24
     */
    @Override
    @Transactional
    public List<TempSubject> gainErrorAll(Integer id) {
        temporaryMapper.deletes(id);
        List<TempSubject> list = new ArrayList<>();
        List<ErrorTopicVo> errorTopics = topicMapper.selectAll(id);
        for (int i = 0; i < errorTopics.size(); i++) {
            TempSubject tempSubject = new TempSubject();
            tempSubject.setSubjectVo(errorTopics.get(i).getSubject());
            tempSubject.setSsid(errorTopics.get(i).getSsid());
            tempSubject.setUid(errorTopics.get(i).getUid());
            tempSubject.setTime(new Date().getTime()/1000+"");
            tempSubject.setTorder(i+1);
            list.add(tempSubject);
        }
        Integer insertResult=temporaryMapper.insertByTempSubject(list);
        return list;
    }

    @Override
    public Integer gainErrorCount(Integer id) {
        return topicMapper.selectByCount(id);
    }

    @Override
    @Transactional
    public Integer addError(ErrorTopic error) {
        return topicMapper.insertSelective(error);
    }

    @Override
    @Transactional
    public Integer removeError(Integer userId) {
        return topicMapper.deletes(userId);
    }

    @Override
    public ErrorTopic isExist(ErrorTopic error) {
        return topicMapper.selectBySsidAndUserId(error);
    }
}
